using System;
using System.Collections.Generic;
using System.Text;
using Danser_Entity;

using Danser_Control.Managers;
using Danser_Entity.Entities;
using Danser_Entity.Queries.Filters;
using Danser_Entity.Queries.Data;

namespace Danser_Control.ManagersImpl
{
    /// <summary>
    /// implements the IQueryManager interface
    /// </summary>
    public class QueryManagerImpl : IQueryManager
    {
        #region C'tors
        
        /// <summary>
        /// creates a QueryManagerImpl instance
        /// </summary>
        public QueryManagerImpl() { }
        
        #endregion

        #region Private Fields

        private IDBManager manager = new DBManager();

        #endregion

        #region IQueryManager Members

        /// <see cref="Danser_Control.Managers.IQueryManager.GetTests"/>
        public IList<TestInfo> GetTests(String filter)
        {
            return manager.GetTestInfo(filter);
        }

        /// <see cref="Danser_Control.Managers.IQueryManager.GetTestResults"/>
        public IList<TestResults> GetTestResults(String filter)
        {
            return manager.GetTestResults(filter);
        }

        /// <see cref="Danser_Control.Managers.IQueryManager.GetTestData"/>
        public IList<TestIntegratedData> GetTestData(String filter)
        {
            return manager.GetTestData(filter);
        }

        /// <see cref="Danser_Control.Managers.IQueryManager.BuildGraph"/>
        public void BuildGraph(TestResults results, IList<string> yAxis)
        {
            throw new NotImplementedException();
        }

        /// <see cref="Danser_Control.Managers.IQueryManager.SaveQuery"/>
        public void SaveQuery(string queryName, string queryDescription, string queryString, string userID)
        {
            UserQuery userQuery = new UserQuery(queryName, queryDescription, queryString, userID);
            manager.AddUserQuery(userQuery);
        }

        /// <see cref="Danser_Control.Managers.IQueryManager.GetUserSpecificQuery"/>
        public UserQuery GetUserSpecificQuery(string userID, int queryId)
        {
            String filter = String.Format(DBConstants.QUERIES_USERID + " = {0} and " + DBConstants.QUERIES_ID + " = {1}", userID, queryId);
            List<UserQuery> queries = manager.GetQueries(filter);

            return queries[0];
        }

        /// <see cref="Danser_Control.Managers.IQueryManager.GetUserFavoriteQueries"/>
        public IList<UserQuery> GetUserFavoriteQueries(string userID)
        {
            return manager.GetAllUserQueries(userID);
        }

        #endregion

        #region Heplers:

        /// <summary>
        /// Convert the rule object into filter expression to send to DB
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        private String GetFilter(RuleExpression filter)
        {
            String strFilter = null;

            // filter is null - get all test info:
            if (filter != null)
            {
                // translate the filter to an SQL string:
                strFilter = filter.ConditionsToString(true);

                // if the string is empty, send null instead:
                if (0 == strFilter.Length)
                {
                    strFilter = null;
                }
            }

            return strFilter;
        }

        #endregion

    }

}
